CXX = g++
LIB = -lGL -lEGL -lGLESv2 -lX11 -lpthread
INC = 
CXXFLAGS = -gdwarf-2 -g3 -fPIC -std=gnu++11 -DUSE_OPENGL_ES -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES -Wall -Wunreachable-code
AR = ar

OUTDIR = objs

TARGET = objviewer

INC += -I. -I./src -I./linux \


APP_SRCS := 				\
	src/Application3D.cpp \
	src/CZGeometry.cpp \
	src/CZMat4.cpp \
	src/CZMaterial.cpp \
	src/CZMaterialLib.cpp \
	src/CZObjFileParser.cpp \
	src/CZObjModel.cpp \
	src/CZShader.cpp \
	src/CZUtil.cpp \
	linux/gles.cpp
	

APP_OBJS = $(patsubst %.cpp, ${OUTDIR}/%.o, ${APP_SRCS})

OUTDIRS := $(foreach dir,$(APP_OBJS), $(dir $(dir)))
MKDIRS := $(shell mkdir -p $(OUTDIRS))
DEPS = $(APP_OBJS:.o=.d)

.PHONY:all
all: $(TARGET)

$(TARGET): $(APP_OBJS)
	$(CXX) $(CXXFLAGS) -o $@ $(APP_OBJS) $(LIB)
	
$(OUTDIR)/%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(INC) -o $@ -c $<

$(OUTDIR)/%.d1: %.cpp
	$(CXX) $< -MM $(CXXFLAGS) $(INC) > $@

$(OUTDIR)/%.d: %.cpp
	@echo "Creating $@ ..."
	@set -e; \
        $(CC) -MM -E $(CXXFLAGS) $(INC) $^ | sed 's,\(.*\)\.o[ :]*,$(@:.d=.o) : ,g' > $@

$(OUTDIR)/%.o: %.c
	$(CXX) $(CXXFLAGS) $(INC) -o $@ -c $<

$(OUTDIR)/%.d1: %.c
	$(CXX) $< -MM $(CXXFLAGS) $(INC) > $@

$(OUTDIR)/%.d: %.c
	@echo "Creating $@ ..."
	@set -e; \
        $(CC) -MM -E $(CXXFLAGS) $(INC) $^ | sed 's,\(.*\)\.o[ :]*,$(@:.d=.o) : ,g' > $@

-include $(DEPS)

.PHONY:clean
clean:
	rm -f *.o
	rm -rf $(APP_OBJS)
	rm -rf $(OUTDIR)
	rm -f $(TARGET)
